package com.ebelter.seneo.ui.activitys;


import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.models.googlefit.GoogleFitManager;
import com.ebelter.seneo.models.googlefit.UtilGoogleFitt;
import com.ebelter.seneo.utils.SeneoUtils;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;


public class GoogleFitActivity extends SeneoBaseActivity {


    ImageView google_fit_iv;
    ImageView top_iv;
    TextView top_tv1;

    private Handler mHandler=new Handler();


    GoogleFitManager googleFitManager;
  //  private UtilGoogleFitt utilGoogleFitt = new UtilGoogleFitt(this);
    @Override
    protected int loadLayoutById() {
        return R.layout.activity_google_fit;
    }


    private boolean open;

    @Override
    protected void initView() {
        google_fit_iv=findViewById(R.id.google_fit_iv);
        top_iv=findViewById(R.id.top_iv);
        top_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        top_tv1=findViewById(R.id.top_tv1);
        top_tv1.setText("Google Fit");
        ViewUtils.displayView(top_tv1);

        googleFitManager=GoogleFitManager.getInstance();

        boolean isOpen = SeneoUtils.getGoogleFitOpenFlag();

        GoogleSignInAccount googleSignInAccount=googleFitManager.getLastSignedInAccount(this);

        if (googleSignInAccount==null){
            LogUtils.i2("GoogleFitActivity","googleSignInAccount==null");
        }else {
            LogUtils.i2("GoogleFitActivity","googleSignInAccount!=null");
        }

        setGoogleFitIvStattion(isOpen&&googleSignInAccount!=null);

        google_fit_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isOpen = SeneoUtils.getGoogleFitOpenFlag();
                if (isOpen){
                    //如果是开启的 就要关闭掉
                    setGoogleFitIvStattion(false);//设置图标为关闭状态
                    SeneoUtils.setGoogleFitOpenFlag(false);
                }else {
                    //如果是关闭的 就要开启
                    setGoogleFitIvStattion(true);//设置图标为打开状态
                   // SeneoUtils.setGoogleFitOpenFlag(true);
                    GoogleSignInAccount signInAccount = googleFitManager.getLastSignedInAccount(getApplicationContext());
                        if (signInAccount==null) { //没有。则要登录
                            googleFitManager.signIn(GoogleFitActivity.this);
                        }else{//有，则要订阅
                            SeneoUtils.setGoogleFitOpenFlag(true);
                            googleFitManager.sunbscrerib(GoogleFitActivity.this);
                        }


                }
            }
        });
    }


    private void setGoogleFitIvStattion(boolean open){
        if (open){
            ViewUtils.setImageResource(google_fit_iv,R.drawable.ic_slide_open);
        }else {
            ViewUtils.setImageResource(google_fit_iv,R.drawable.ic_slide_close);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //utilGoogleFitt.handleSignInResult(requestCode, data);

        googleFitManager.handleResult(GoogleFitActivity.this, requestCode, data, new GoogleFitManager.LoginCallback() {
            @Override
            public void success() {
                LogUtils.i2("GoogleFitActivity","GoogleFit 登录成功了");
                setGoogleFitIvStattion(true);
                SeneoUtils.setGoogleFitOpenFlag(true);
            }

            @Override
            public void failure() {
                LogUtils.i2("GoogleFitActivity","GoogleFit 登录失败了");
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        setGoogleFitIvStattion(false);
                        ToastUtil.show("Connection to Google Fit failed");
                    }
                },2000);

            }
        });
    }


    public void insertWeight(View view) {
        long time=System.currentTimeMillis();

        googleFitManager.insertWeight(this, 66.6f, time, time + 1000, new GoogleFitManager.InsertDataCallback() {
            @Override
            public void success() {
                LogUtils.i2("GoogleFitActivity","插入体重成功了");
            }

            @Override
            public void failure(int error, String desc) {
                LogUtils.i2("GoogleFitActivity","插入体重失败了: error="+error+"--desc="+desc);
            }
        });

       // utilGoogleFitt.getGoogleSignIn();
    }

    public void insertFat(View view) {
        long time=System.currentTimeMillis();

        googleFitManager.insertFat(this, 0.88f, time, time + 1000, new GoogleFitManager.InsertDataCallback() {
            @Override
            public void success() {
                LogUtils.i2("GoogleFitActivity","插入Fat成功了");
            }

            @Override
            public void failure(int error, String desc) {
                LogUtils.i2("GoogleFitActivity","插入Fat失败了: error="+error+"--desc="+desc);
            }
        });
    }

    public void insertBmr(View view) {
        long time=System.currentTimeMillis();

        googleFitManager.insertBmr(this, 1024f, time, time + 1000, new GoogleFitManager.InsertDataCallback() {
            @Override
            public void success() {
                LogUtils.i2("GoogleFitActivity","插入BMR成功了");
            }

            @Override
            public void failure(int error, String desc) {
                LogUtils.i2("GoogleFitActivity","插入BMR失败了: error="+error+"--desc="+desc);
            }
        });
    }
}
